/*
 * ControlTween - "in-betweening" animation of Control elements
 *
 * Copyright 2007 James W. Hofmann
 *
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */
class ControlTween
{

	public var curframe : Int;
	
	public var startx : Float;
	public var starty : Float;
	public var endx : Float;
	public var endy : Float;
	public var transitiontype : String;
	public var transitionlength : Int;
	public var transitionvars : Dynamic;

	public var parent : Control;
	
	public function new(parent : Control)
	{
		curframe = 0;
		startx = parent.left;
		starty = parent.top;
		endx = parent.left;
		endy = parent.top;
		transitiontype = "";
		transitionlength = -1;
		transitionvars = null;
		this.parent = parent;
		parent.tween = this;
	}

	public function setStart(left : Float, top : Float)
	{
		startx = left;
		starty = top;
	}

	public function setEnd(left : Float, top : Float)
	{
		endx = left;
		endy = top;
	}

	public function up()
	{ 
		if (curframe<=transitionlength)
		{
			switch(transitiontype)
			{
				case("linear"):
					var diffx = endx - startx;
					var diffy = endy - starty;
					var transition = curframe/transitionlength;
					diffx = diffx * transition;
					diffy = diffy * transition;
					parent.setLeft(startx+diffx);
					parent.setTop(starty+diffy);
				case("speeding"):
					var diffx = endx - startx;
					var diffy = endy - starty;
					var transition = curframe/transitionlength;
					var factor = transition*transition;
					diffx = diffx * factor;
					diffy = diffy * factor;
					parent.setLeft(startx+diffx);
					parent.setTop(starty+diffy);
				case("slowing"):
					var diffx = endx - startx;
					var diffy = endy - starty;
					var transition = curframe/transitionlength;
					var factor = (1-(1-transition)*(1-transition));
					diffx = diffx * factor;
					diffy = diffy * factor;
					parent.setLeft(startx+diffx);
					parent.setTop(starty+diffy);
				case("bounceinto"):
					var diffx = endx - startx;
					var diffy = endy - starty;
					var accelpct = 1/transitionlength;
					var accelx = diffx*accelpct;
					var accely = diffy*accelpct;
					var nextx = startx;
					var nexty = starty;
					var speedx = accelx;
					var speedy = accely;
					if (curframe==transitionlength)
					{
						nextx = endx;
						nexty = endy;
					}
					else
					{
						for (n in 0...curframe)
						{
							speedx+=accelx;
							speedy+=accely;
							nextx+=speedx;
							nexty+=speedy;
							if (accelx>0 || accely>0)
							{
								if (nextx-endx>0 || nexty-endy>0 )
								{
									speedx = -speedx * 0.8;
									speedy = -speedy * 0.8;
									nextx = endx;
									nexty = endy;
								}
							}
							else
							{
								if (nextx-endx<0 || nexty-endy<0 )	
								{						
									speedx = -speedx * 0.8;	
									speedy = -speedy * 0.8;	
									nextx = endx;
									nexty = endy;
								}
							}
						}
					}
					parent.setLeft(nextx);
					parent.setTop(nexty);
				case("jumpandreturn"):
					var diffx = 2*(endx - startx);
					var diffy = 2*(endy - starty);
					var transition = curframe/transitionlength;
					var factor = transition*(1-transition);
					diffx = diffx * factor;
					diffy = diffy * factor;
					parent.setLeft(startx+diffx);
					parent.setTop(starty+diffy);
			}
			curframe+=1;
		}
	}
	
}
